<?php
/**
 *author:韩帅
 *createtime:2017/6/21 14:17
 *description:
 */
namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class UserServerBalance extends Model
{
    /**
     * 定义主键字段
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * 定义表名，不含前缀
     *
     * @var string
     */
    protected $table      = 'wl_user_server_balance_log';

    /**
     * 定义拒绝保存的字段
     *
     * @var array
     */
    protected $guarded    = ['id'];

    /**
     * 开启laravel默认的时间记录
     *
     * @var bool
     */
    public $timestamps = true;

    /**
     * 定义银行卡提现类型
     */
    const TYPE_WIDTHDRAW = 0;

    /**
     * 定义收入类型
     */
    const ACTION_TYPE_IN = 2;

    /**
     * 定义支出类型
     */
    const ACTION_TYPE_GO = 1;

    /**
     * 定义状态
     */
    const STATUS_ONE    = 0;

    /**
     * 定义状态
     */
    const STATUS_TWO    = 1;

    /**
     * 定义状态
     */
    const STATUS_THREE  = 2;

    /**
     * 定义状态
     */
    const STATUS_FOUR   = 3;

    /**
     * @var array 订单类型
     */
    public $type       = ['0' => '银行卡提现','1' => '退货','2' => '订单返利','3' => '购买消费'];

    /**
     * 收入支出类型
     */
    private $action_type = ['0' => '其他','1' => '支出','2' => '收入'];

    /**
     * 订单状态
     */
    private $status     = ['0' => '待审核','1' => '审核中','2' => '审核通过','3' => '审核失败'];

    /**
     * 处理交易明细列表
     */
    public  function translate($data)
    {
        $newArr                 = [];
        $newArr['total']        = $data['total'];
        $newArr['per_page']     = $data['per_page'];
        $newArr['current_page'] = $data['current_page'];
        $newArr['last_page']    = $data['last_page'];

        foreach ($data['data'] as &$v) {

            if (empty($v['order_sn'])) {

                $v['order_sn'] = '';
            }

            if (empty($v['money'])) {

                $v['money'] = 0.00;
            }else {

                if ($v['action_type'] === self::ACTION_TYPE_GO) {
                    $v['money'] = '-'.number_format($v['money'],2);
                }
                if ($v['action_type'] === self::ACTION_TYPE_IN) {
                    $v['money'] = '+'.number_format($v['money'],2);
                }
            }

//            if (!empty($v['type'])) {
//
//                $v['type'] = $this->type[$v['type']];
//            }
//
//            if (!empty($v['action_type'])) {
//
//                $v['action_type'] = $this->action_type[$v['action_type']];
//            }

//            $v['status'] = $this->status[intval($v['status'])];

            if (empty($v['note'])) {

                $v['note'] = '';
            }

            $newArr['balance_log'][] = ['order_sn'    => $v['order_sn'] , 'money'   => $v['money'],'type'        => $v['type'],
                                        'action_type' => $v['action_type'],'status' => $v['status'],'created_at' => $v['created_at'],
                                        'icon'        => '','note' => $v['note']
                                        ];
        }

        return $newArr;
    }

    /**
     * 获取银行卡号和类型
     */
    public static function getCard($id) {

        $info = [];
        $info = DB::table('wd_user_card')->join('wd_card_type','wd_user_card.card_type','=','wd_card_type.id')
                                         ->select('wd_user_card.card_num','wd_card_type.name')
                                         ->first();
        return $info;
    }

    /**
     * 订单支付成功,新增一条记录
     */
    public static function insertLog($user_id,$order_sn,$money) {

        $params = [];

        $user_id  == null || $params['user_id']  = $user_id;
        $order_sn == null || $params['order_sn'] = $order_sn;
        $money    == null || $params['money']    = $money;

        if (count($params) <3 ) {

            return false;
        }
        $params['created_at']  = date('Y-m-d H:i:s', time());
        $params['updated_at']  = date('Y-m-d H:i:s', time());
        $params['type']        = 2;
        $params['action_type'] = 1;

        $res = DB::table('wl_user_server_balance_log')->insert($params);

        if ($res == true) {

            return true;
        }
        return false;
    }
}
